package com.gopay.common.event;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.gopay.common.domain.cps.CpsBatchPayTask;
import com.gopay.common.domain.cps.CpsGenMainOrder;
import com.gopay.common.domain.report.Event;

/**
 * cps订单事件发送者
 * 
 * @CpsOrderEventSender.java
 * @author fanghw
 * @2015年6月4日 上午10:17:49  www.gopay.com.cn Inc.All rights reserved.
 */
@Service(value = "cpsOrderEventSender")
public class CpsOrderEventSender {
    private final static Logger logger = LoggerFactory.getLogger(CpsOrderEventSender.class);
    
    @Resource(name = "eventCollector")
    private EventCollector eventCollector;
    
    /**
     * 发送主订单保存事件
     * @param mainOrder
     */
    public void sendSaveEvent(CpsGenMainOrder mainOrder){
        logger.error("***************************发送主订单保存事件,gopayOrderId="+mainOrder.getGopayOrderId());
        
        collectMainOrderEvent(mainOrder,ReportEventType.ORDER_SAVE);
    }
    
    /**
     * 发送主订单更新事件
     * @param mainOrder
     */
    public void sendUpdateEvent(CpsGenMainOrder mainOrder){
        logger.error("***************************发送主订单更新事件,gopayOrderId="+mainOrder.getGopayOrderId());
        
        collectMainOrderEvent(mainOrder,ReportEventType.ORDER_UPDATE);
    }
    
    /**
     * 发送主订单删除事件
     * @param mainOrder
     */
    public void sendDeleteEvent(CpsGenMainOrder mainOrder){
        logger.error("***************************发送主订单删除事件,gopayOrderId="+mainOrder.getGopayOrderId());
        
        collectMainOrderEvent(mainOrder,ReportEventType.ORDER_DELETE);
    }
    
    /**
     * 发送批量付款任务保存事件
     * @param batchPayTask
     */
    public void sendSaveEvent(CpsBatchPayTask batchPayTask){
        logger.error("***************************发送批量付款任务保存事件,batchTaskId="+batchPayTask.getBatchTaskId());
        
        collectBatchPayTaskEvent(batchPayTask,ReportEventType.TASK_SAVE);
    }
    
    /**
     * 发送cps批量付款任务更新事件
     * @param batchPayTask
     */
    public void sendUpdateEvent(CpsBatchPayTask batchPayTask){
        logger.error("***************************发送批量付款任务更新事件,batchTaskId="+batchPayTask.getBatchTaskId());
        
        collectBatchPayTaskEvent(batchPayTask,ReportEventType.TASK_UPDATE);
    }
    
    /**
     * 发送创建审批任务事件
     * @param batchPayTask
     */
    public void sendApprovalTaskEvent(String gopayOrderId){
        logger.error("***************************发送创建审批任务事件,gopayOrderId="+gopayOrderId);
        
        collectOrderApprovalEvent(gopayOrderId,ReportEventType.APPROVAL_TASK);
    }
    
    /**
     * 发送执行审批决议事件
     * @param batchPayTask
     */
    public void sendApprovalDecisionEvent(String gopayOrderId){
        logger.error("***************************发送执行审批决议事件,gopayOrderId="+gopayOrderId);
        
        collectOrderApprovalEvent(gopayOrderId,ReportEventType.APPROVAL_DECISION);
    }
    
    /**
     * 发送冻结余额事件
     * @param acctId
     */
    public void sendFrezBalEvent(String acctId){
        logger.error("***************************发送冻结余额事件,acctId="+acctId);
        
        collectFrezAndUnfrezBalEvent(acctId,ReportEventType.FREZ_BAL);
    }
    
    /**
     * 发送解冻余额事件
     * @param gopayOrderId
     */
    public void sendUnfrezBalEvent(String acctId){
        logger.error("***************************发送解冻余额事件,acctId="+acctId);
        
        collectFrezAndUnfrezBalEvent(acctId,ReportEventType.UN_FREZ_BAL);
    }
    
    /**
     * 收集主订单事件
     * @param mainOrder
     * @param eventType
     */
    private void collectMainOrderEvent(CpsGenMainOrder mainOrder,ReportEventType eventType){
       try{
           eventCollector.start();
           
           // 封装事件
           Event event = new Event();
           event.setType(eventType.value);
           event.setGopayOrderId(mainOrder.getGopayOrderId());

           Map<String, Object> map = new HashMap<String, Object>();
           map.put("gopayOrderId", mainOrder.getGopayOrderId());
           event.setSnapshot(JSONObject.toJSONString(map, SerializerFeature.WriteClassName));
           eventCollector.collect(event);
       }catch(Exception e){
           //吃掉所有异常
           logger.error("收集主订单事件 Error,gopayOrderId="+mainOrder.getGopayOrderId()+",eventType="+eventType.value, e);
       }
    }
    
    /**
     * 收集批量付款任务事件
     * @param mainOrder
     * @param eventType
     */
    private void collectBatchPayTaskEvent(CpsBatchPayTask batchPayTask,ReportEventType eventType){
        try{
            eventCollector.start();
                    
            // 封装事件
            Event event = new Event();
            event.setType(eventType.value);
            event.setBatchTaskId(String.valueOf(batchPayTask.getBatchTaskId()));
    
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("batchTaskId", batchPayTask.getBatchTaskId());
            event.setSnapshot(JSONObject.toJSONString(map, SerializerFeature.WriteClassName));
            
            eventCollector.collect(event);
        }catch(Exception e){
            //吃掉所有异常
            logger.error("收集批量付款任务事件 Error,batchTaskId="+batchPayTask.getBatchTaskId()+",eventType="+eventType.value, e);
        }
    }
    
    /**
     * 收集订单审批事件
     * @param gopayOrderId
     * @param eventType
     */
    private void collectOrderApprovalEvent(String gopayOrderId,ReportEventType eventType){
        try{
            eventCollector.start();
                    
            // 封装事件
            Event event = new Event();
            event.setType(eventType.value);
            event.setGopayOrderId(gopayOrderId);
    
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("gopayOrderId", gopayOrderId);
            event.setSnapshot(JSONObject.toJSONString(map, SerializerFeature.WriteClassName));
            
            eventCollector.collect(event);
        }catch(Exception e){
            //吃掉所有异常
            logger.error("收集订单审批事件 Error,gopayOrderId="+gopayOrderId+",eventType="+eventType.value, e);
        }
    }
    
    /**
     * 收集冻结/解冻余额事件
     * @param gopayOrderId
     * @param eventType
     */
    private void collectFrezAndUnfrezBalEvent(String acctId,ReportEventType eventType){
        try{
            eventCollector.start();
                    
            // 封装事件
            Event event = new Event();
            event.setType(eventType.value);
    
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("acctId", acctId);
            event.setSnapshot(JSONObject.toJSONString(map, SerializerFeature.WriteClassName));
            
            eventCollector.collect(event);
        }catch(Exception e){
            //吃掉所有异常
            logger.error("收集冻结/解冻余额事件 Error,acctId="+acctId+",eventType="+eventType.value, e);
        }
    }
}
